package net.hn.hnms.biz.safety.location.group.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.api.safety.gasext.domain.OverstaffingSituationDwsVo;
import net.hn.hnms.biz.api.safety.gasext.fegin.RemoteDwsService;
import net.hn.hnms.biz.safety.location.domain.ToDayDataTotal;
import net.hn.hnms.biz.safety.location.group.domain.OnLineMineInfoVo;
import net.hn.hnms.biz.safety.location.group.domain.OverstaffingSituationVo;
import net.hn.hnms.biz.api.safety.gasext.domain.PersonnelMonitoringVo;
import net.hn.hnms.biz.safety.location.group.domain.dto.Dataset;
import net.hn.hnms.biz.safety.location.group.domain.dto.KengXiaNumber;
import net.hn.hnms.biz.safety.location.group.service.GroupLocationService;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.redis.utils.RedisUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 智呈物联 pan'sx
 * 2022/12/20 15:59
 * 1.0 version
 */
@RestController
@RequestMapping("group/riskMonitor/location")
@Api(value = "人员定位控制器", tags = {"人员定位-集团端"})
@RequiredArgsConstructor
public class GroupLocationController {
    private final GroupLocationService groupLocationService;

    @Resource
    private RemoteDwsService remoteDwsService;

    @GetMapping("todayData")
    @ApiOperation("今日人/车")
    public R<ToDayDataTotal> monitoring(String companyCode ) {
        return R.ok(groupLocationService.toDayCount( companyCode));
    }


    /**
     * 获取大屏人员监测信息
     * @return R
     */
    @GetMapping("getPersonnelMonitoring")
    @ApiOperation("大屏首页-人员检测")
    public R<PersonnelMonitoringVo> getPersonnelMonitoring(@ApiParam("公司编码") String companyCode,@ApiParam("煤矿编码") String mineCode) {
        PersonnelMonitoringVo vo = new PersonnelMonitoringVo();
        Boolean istru = RedisUtils.getCacheObject(Constants.SYS_DWS_KEY);
        if (istru == null || !istru) {
             vo = groupLocationService.getPersonnelMonitoring();
        } else {
             vo = remoteDwsService.getPersonnelMonitoring(companyCode,mineCode).getData();
        }
        return R.ok(vo);
    }


    /**
     * 超员情况
     * @return R
     */
    @GetMapping("getOverstaffingSituation")
    @ApiOperation("大屏首页-超员情况")
    public R<List<OverstaffingSituationVo>> getOverstaffingSituation(@ApiParam("公司编码") String companyCode, @ApiParam("煤矿编码") String mineCode) {
        List<OverstaffingSituationVo> personnelMonitoringVoList = new ArrayList<>();

        Boolean istru = RedisUtils.getCacheObject(Constants.SYS_DWS_KEY);
        if (istru == null || !istru) {
            personnelMonitoringVoList = groupLocationService.getOverstaffingSituation();
        } else {
            List<OverstaffingSituationDwsVo> data = remoteDwsService.getOverstaffingSituation(companyCode,mineCode).getData();
            for (OverstaffingSituationDwsVo datum : data) {
                OverstaffingSituationVo overstaffingSituationVo = new OverstaffingSituationVo();
                BeanUtils.copyProperties(datum,overstaffingSituationVo);
                personnelMonitoringVoList.add(overstaffingSituationVo);
            }

        }

        return R.ok(personnelMonitoringVoList);
    }

    /**
     * 人员监测联网情况
     * @return R
     */
    @GetMapping("getOnLineMineInfo")
    @ApiOperation("大屏首页-人员监测联网情况")
    public R<OnLineMineInfoVo> getOnLineMineInfo() {
        OnLineMineInfoVo OnLineMineInfoVo = groupLocationService.getOnLineMineInfo();
        return R.ok(OnLineMineInfoVo);
    }

    @GetMapping("getStripMineLoginToken")
    @ApiOperation("获取露天矿登录接口")
    public R<String> getStripMineLoginToken(String ip) {
        String loginToken = groupLocationService.getLoginToken(ip);
        return R.ok(loginToken);
    }
    @GetMapping("getStripMineKengxiaNumber")
    @ApiOperation("获取露天矿井下人数")
    public R<KengXiaNumber> getStripMineKengxiaNumber(String ip, String token) {
        KengXiaNumber numbr = groupLocationService.getKengXiaNumber(ip, token);
        return R.ok(numbr);
    }
    @GetMapping("getStripMineZhiBanInfo")
    @ApiOperation("获取露天矿值班信息")
    public R<Dataset> getStripMineZhiBanInfo(String ip,String token) {
        Dataset kengZhiBanLeaderInfo = groupLocationService.getKengZhiBanLeaderInfo(ip, token);
        return R.ok(kengZhiBanLeaderInfo);
    }
}
